package ui.activity.login.presenter;


import com.google.gson.Gson;

import javax.inject.Inject;

import Bean.ModifyPasswordBean;
import Bean.ModifyPasswordParamBean;
import Bean.SendPhoneCodeBean;
import Bean.SendPhoneCodeParamBean;
import base.BaseBiz;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import ui.activity.login.biz.ForgetPwdBiz;
import ui.activity.login.contract.ForgetPwdContract;
import util.StringUtils;

/**
 * Created by chenjian on 19/2/26
 */

public class ForgetPwdPresenter implements ForgetPwdContract.Presenter {

    ForgetPwdContract.View view;
    ForgetPwdBiz forgetPwdBiz;

    String mCode = "";
    String mPhone = "";


    @Inject
    public ForgetPwdPresenter(ForgetPwdContract.View view) {
        this.view = view;
    }


    @Override
    public void setBiz(BaseBiz biz) {
        forgetPwdBiz = (ForgetPwdBiz) biz;
    }


    @Override
    public void getPwdInfo(String phone, String passWord, String rePassWord) {
        if (StringUtils.isEmpty(passWord) || StringUtils.isEmpty(rePassWord)) {
            view.showMsg("请填写密码");
            return;
        }
        if (!passWord.equals(rePassWord)) {
            view.showMsg("两次填写的密码不一致");
            return;
        }
        view.loading(true);
        ModifyPasswordParamBean m = new ModifyPasswordParamBean();
        m.setUserName(phone);
        m.setPassWord(passWord);
        m.setRePassWord(rePassWord);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), new Gson().toJson(m));
        forgetPwdBiz.getPwd(requestBody, new BaseBiz.Callback<ModifyPasswordBean.DataBean>() {
            @Override
            public void onSuccess(ModifyPasswordBean.DataBean info) {
                view.showMsg("重置密码成功");
                view.loading(false);
                view.finishAct();
            }

            @Override
            public void onFailure(ModifyPasswordBean.DataBean info) {
                view.loading(false);
            }
        });

    }

    @Override
    public void toNext(String phone, String code) {
        if (StringUtils.isEmpty(phone)) {
            view.showMsg("手机号码为填写");
            return;
        }
//        if (!CheckPhoneNumberUtils.isMobileNO(phone)) {
//            view.showMsg("请填写正确的手机号");
//            return;
//        }
        if (!mPhone.equals(phone)) {
            view.showMsg("手机号与获取验证码手机号不一致");
            return;
        }
        if (!mCode.equals(code)) {
            view.showMsg("验证码不正确");
            return;
        }
        view.upDateType();
    }


    @Override
    public void getPhoneCode(String phone) {
        if (StringUtils.isEmpty(phone)) {
            view.showMsg("请填写手机号码");
            return;
        }
//        if (!CheckPhoneNumberUtils.isMobileNO(phone)) {
//            view.showMsg("请填写正确的手机号");
//            return;
//        }
        view.loading(true);
        SendPhoneCodeParamBean sendPhoneCodeParamBean = new SendPhoneCodeParamBean();
        sendPhoneCodeParamBean.setTelephoneNumber(phone);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), new Gson().toJson(sendPhoneCodeParamBean));
        forgetPwdBiz.getPhoneCode(requestBody, new BaseBiz.Callback<SendPhoneCodeBean.DataBean>() {
            @Override
            public void onSuccess(SendPhoneCodeBean.DataBean info) {
                view.showMsg("获取验证码成功");
                mCode = info.getCode();
                mPhone = phone;
                view.upDateUI();
                view.loading(false);
            }

            @Override
            public void onFailure(SendPhoneCodeBean.DataBean info) {
                view.loading(false);
            }
        });
    }


}
